home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 24 / AACD 24.iso / AACD / Programming / imageioproc.lib / dev / examples / autogamma / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-08-17  |  5.2 KB  |  213 lines

  1. /* This example use of imageprocess.library loads the image file specified
  2.         on the command line at half size and performs autogamma on it and views
  3.         it halved in size in a guigfx window.
  4. */
  5.  
  6. #include <stdio.h>
  7.  
  8. #include <dos/dos.h>
  9. #include <exec/memory.h>
  10. #include <exec/types.h>
  11.  
  12. #include <clib/dos_protos.h>
  13. #include <clib/exec_protos.h>
  14. #include <clib/intuition_protos.h>
  15.  
  16. #include <pragmas/dos_pragmas.h>
  17. #include <pragmas/exec_pragmas.h>
  18. #include <pragmas/intuition_pragmas.h>
  19.  
  20. #include <imageio/imageio.h>
  21. #include <imageio/imageio_protos.h>
  22. #include <imageio/imageio_pragmas.h>
  23.  
  24. #include <imageprocess/imageprocess.h>
  25. #include <imageprocess/imageprocess_protos.h>
  26. #include <imageprocess/imageprocess_pragmas.h>
  27.  
  28. #include <guigfx/guigfx.h>
  29. #include <pragmas/guigfx_pragmas.h>
  30. #include <guigfx/guigfx_protos.h>
  31.  
  32. /* Function prototypes */
  33. __saveds __asm ULONG progressFunc( register __d0 ULONG curr, register __d1 ULONG lines, register __a0 void *userdata );
  34. void DisplayGuiGfx( UBYTE *buffer, UBYTE cs, ULONG bpp, ULONG x, ULONG y );
  35.  
  36. extern struct Library *DOSBase;
  37. struct Library *ImageIOBase, *IntuitionBase, *ImageProcessBase;
  38.  
  39. void main( int argc, char **argv )
  40. {
  41.     if ( argv[1] )
  42.     {
  43.         ImageIOBase = OpenLibrary( "imageio.library", 2 );
  44.         ImageProcessBase = OpenLibrary( "imageprocess.library", 1 );
  45.         IntuitionBase = OpenLibrary( "intuition.library", NULL );
  46.         if ( IntuitionBase && ImageIOBase && ImageProcessBase )
  47.         {
  48.             struct ImageHandle *ih;
  49.             ULONG err;
  50.  
  51.             err = AllocImage( &ih,
  52.                 IMG_SrcFilename, argv[1],
  53.                 TAG_DONE );
  54.             if ( !err )
  55.             {
  56.                 ULONG num = 1, denom = 2;
  57.                 ULONG x, y, bpp;
  58.                 UBYTE *buffer, cs;
  59.                 int prevpercent;
  60.  
  61.                 err = GetImageAttrs( ih,
  62.                     IMG_Width, &x,
  63.                     IMG_Height,&y,
  64.                     IMG_BytesPerPixel, &bpp,
  65.                     IMG_ColourSpace, &cs,
  66.                     IMG_TestScaleNum, num,
  67.                     IMG_TestScaleDenom, denom,
  68.                     TAG_DONE );
  69.                 if ( !err )
  70.                 {
  71.                     printf( "width=%ld, height=%ld\n", x, y );
  72.                     printf( "bytes per pixel=%ld, colourspace=%d\n", bpp, cs );
  73.  
  74.                     prevpercent = 0;
  75.  
  76.                     err = ReadImage( ih,
  77.                         IMG_ScaleNum, num,
  78.                         IMG_ScaleDenom, denom,
  79.                         IMG_ImageBuffer, &buffer,
  80.                         IMG_ProgressHook, progressFunc,
  81.                         IMG_ProgressUserData, &prevpercent,
  82.                         TAG_DONE );
  83.                     if ( !err )
  84.                     {
  85.                         ULONG iperr;
  86.  
  87.                         printf("imageprocess.library\n");
  88.  
  89.                         iperr = DoImageProcess( IMP_Process_AutoGamma,
  90.                             IMP_SourceImageIOHandle, ih,
  91.                             IMP_GammaMode, GAMMA_FAST,
  92.                             TAG_DONE );
  93.                         if ( !iperr )
  94.                         {
  95.                             printf("%ld x %ld\n",x,y);
  96.  
  97.                             err = GetImageAttrs( ih,
  98.                                 IMG_ImageBuffer, &buffer,
  99.                                 TAG_DONE );
  100.                             if ( !err )
  101.                             {
  102.                                 DisplayGuiGfx( buffer, cs, bpp, x, y );
  103.                             }
  104.                         }
  105.                         else printf( "doimageprocess error:%ld\n", iperr );
  106.                     }
  107.                     else printf( "read image error:%d\n", err );
  108.                 }
  109.                 else printf( "get image attrs error:%d\n", err );
  110.  
  111.                 FreeImage( ih );
  112.             }
  113.             else printf( "alloc image error:%d\n", err );
  114.         }
  115.  
  116.         if ( ImageIOBase ) CloseLibrary( ImageIOBase );
  117.         if ( ImageProcessBase ) CloseLibrary( ImageProcessBase );
  118.         if ( IntuitionBase ) CloseLibrary ( IntuitionBase );
  119.     }
  120. }
  121.  
  122. __saveds __asm ULONG progressFunc( register __d0 ULONG curr, register __d1 ULONG lines, register __a0 void *userdata )
  123. {
  124.     int *prevpercent = (int *)userdata;
  125.  
  126.     int percent = ( curr * 100 ) / lines;
  127.  
  128.     if ( *prevpercent != percent )
  129.     {
  130.         if ( percent % 10 == 0 ) printf( "%d%%\n", percent );
  131.     }
  132.  
  133.     *prevpercent = percent;
  134.  
  135.     return NULL;
  136. }
  137.  
  138. void DisplayGuiGfx( UBYTE *buffer, UBYTE cs, ULONG bpp, ULONG x, ULONG y )
  139. {
  140.     struct Library *GuiGFXBase;
  141.  
  142.     GuiGFXBase = OpenLibrary( "guigfx.library", NULL );
  143.     if ( GuiGFXBase )
  144.     {
  145.         struct Window *win;
  146.         APTR dh, pi;
  147.  
  148.         win = OpenWindowTags( NULL,
  149.             WA_Title, "Proof",
  150.             WA_Flags, WFLG_ACTIVATE | WFLG_SIMPLE_REFRESH |
  151.                 WFLG_SIZEGADGET | WFLG_RMBTRAP | WFLG_DRAGBAR |
  152.                 WFLG_DEPTHGADGET | WFLG_CLOSEGADGET,
  153.             WA_IDCMP, IDCMP_CLOSEWINDOW | IDCMP_REFRESHWINDOW |
  154.                 IDCMP_SIZEVERIFY | IDCMP_NEWSIZE | IDCMP_RAWKEY,
  155.             WA_Left, 16,
  156.             WA_Top, 16,
  157.             WA_Width, x,
  158.             WA_Height, y,
  159.             TAG_DONE );
  160.  
  161.         if ( win != NULL )
  162.         {
  163.             dh = ObtainDrawHandle( NULL, win->RPort, win->WScreen->ViewPort.ColorMap, TAG_DONE );
  164.  
  165.             if ( dh )
  166.             {
  167.                 UBYTE *newbuffer;
  168.                 ULONG iperr, newbuffersize;
  169.  
  170.                 iperr = DoImageProcess( IMP_Process_SwapColourSpace,
  171.                     IMP_NewColourSpace, IMCS_ARGB,
  172.                     IMP_SourceBuffer, buffer,
  173.                     IMP_SourceWidth, x,
  174.                     IMP_SourceHeight, y,
  175.                     IMP_SourceBytesPerPixel, bpp, 
  176.                     IMP_SourceColourSpace, cs,
  177.                     IMP_Buffer, &newbuffer,
  178.                     IMP_BufferSize, &newbuffersize,
  179.                     TAG_DONE );
  180.                 if ( !iperr )
  181.                 {
  182.                     pi = MakePicture( newbuffer, x, y, GGFX_PixelFormat, PIXFMT_0RGB_32, TAG_DONE );
  183.                     if ( pi )
  184.                     {
  185.                         struct Message *msg;
  186.  
  187.                         DrawPicture( dh, pi, 0, 0, NULL );
  188.  
  189.                         Wait( 1L << win->UserPort->mp_SigBit );
  190.  
  191.                         while ( ( msg = GetMsg( win->UserPort ) ) != NULL ) ReplyMsg( msg );
  192.  
  193.                         DeletePicture( pi );
  194.                     }
  195.                     else printf( "failed to create picture\n" );
  196.  
  197.                     FreeMem( newbuffer, newbuffersize );
  198.                 }
  199.                 else printf( "pic imageprocess err:%ld\n", iperr );
  200.  
  201.                 ReleaseDrawHandle( dh );
  202.             }
  203.             else printf( "failed to get drawhandle\n" );
  204.  
  205.             CloseWindow( win );
  206.         }
  207.         else printf( "failed to open window\n" );
  208.     }
  209.     else printf( "failed to open guigfx.library\n" );
  210.  
  211.     if ( GuiGFXBase ) CloseLibrary( GuiGFXBase );
  212. }
  213.